from django.db import models


# Create your models here.
class Book(models.Model):
    title = models.CharField(max_length=8)
    price = models.IntegerField()
    book_type = models.SmallIntegerField(choices=((1, '新书'), (2, '旧书')), default=1)
    # 关键多对一的publish
    publish = models.ForeignKey(to='Publish', on_delete=models.DO_NOTHING)

    # 展示publish 详细信息
    def publish_detail(self):
        return {'name': self.publish.name, 'city': self.publish.city}


class Publish(models.Model):
    name = models.CharField(max_length=10)
    city = models.CharField(max_length=8)

    # 对关联的字段形成展示,如果不写则返回一个object对象
    def __str__(self):
        return self.name


class UserInfo(models.Model):
    username = models.CharField(max_length=32)
    password = models.CharField(max_length=32)
    user_type = models.IntegerField(choices=((1, '超级管理员'), (2, '普通用户')), default=1)


class TokenInfo(models.Model):
    user = models.OneToOneField(to='UserInfo', on_delete=models.CASCADE)
    token = models.CharField(max_length=128)
